package com.ctsi.flowable.idm.remote;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.flowable.engine.service.FlowableService;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.User;
import org.flowable.idm.engine.impl.UserQueryImpl;

import java.util.List;

/**
 * @author: zhangjw
 * @Date: 2018/10/16 11:31
 * @Description:
 */
public class RemoteUserQueryImpl extends UserQueryImpl {
    private static final long serialVersionUID = -3317063599429696075L;

    private final FlowableService userServiceProxy;

    public RemoteUserQueryImpl(FlowableService userServiceProxy) {
        this.userServiceProxy = userServiceProxy;
    }

    @Override
    public long executeCount(CommandContext commandContext) {
        return this.userServiceProxy.findUserCountByQueryCriteria(toUserQuery());
    }

    @Override
    public List<User> executeList(CommandContext commandContext) {
        return FlowableIdmUtils.toUserEntity(this.userServiceProxy.findUserByQueryCriteria(toUserQuery()));
    }

    private com.ctsi.flowable.idm.dto.UserQuery toUserQuery(){
        com.ctsi.flowable.idm.dto.UserQuery query = new com.ctsi.flowable.idm.dto.UserQuery();

        if (UtilValidate.isNotEmpty(getId())) {
            query.setId(getId());
        }
        if (UtilValidate.isNotEmpty(getIds())) {
            query.setIds(getIds());
        }
        if (UtilValidate.isNotEmpty(getFullNameLike())) {
            query.setFullNameLike(getFullNameLike());
        }
        if (UtilValidate.isNotEmpty(getGroupIds())) {
            query.setGroupIds(getGroupIds());
        }
        if (UtilValidate.isNotEmpty(getGroupId())) {
            query.setGroupId(getGroupId());
        }
        return query;
    }

}
